package com.itheima.web.controller.store;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.City;
import com.itheima.domain.store.Product;
import com.itheima.service.store.ProductService;
import com.itheima.service.store.impl.ProductServiceImpl;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/store/product")
public class ProductServlet extends BaseServlet {

    public void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //进入列表页
        //获取数据
        int page = 1;
        int size = 5;
        if(StringUtils.isNotBlank(request.getParameter("page"))){
            page = Integer.parseInt(request.getParameter("page"));
        }
        if(StringUtils.isNotBlank(request.getParameter("size"))){
            size = Integer.parseInt(request.getParameter("size"));
        }
        PageInfo all = productService.findAll(page, size);
        //将数据保存到指定的位置
        request.setAttribute("page",all);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/product/product-list.jsp").forward(request,response);
    }

    public void toAdd(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        ProductService productService = new ProductServiceImpl();
        List<City> cities = productService.findAllCity();
//       将查找到的cities返回给前端
        request.setAttribute("cities",cities);
        //String id = request.getParameter("id");
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/product/product-add.jsp").forward(request,response);
    }

    public void save(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //将数据获取到，封装成一个对象
        Product product = BeanUtil.fillBean(request,Product.class,"yyyy-MM-dd HH:mm");
        //调用业务层接口save
        productService.save(product);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/store/product?operation=list");
    }

    public void toEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //查询要修改的数据findById
        String id = request.getParameter("id");
        Product product = productService.findById(id);
        //将数据加载到指定区域，供页面获取
        List<City> cities = productService.findAllCity();
//       将查找到的cities返回给前端
        request.setAttribute("cities",cities);
        request.setAttribute("product",product);
        //跳转页面
        request.getRequestDispatcher("/WEB-INF/pages/store/product/product-update.jsp").forward(request,response);
    }

    public void edit(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Product product = BeanUtil.fillBean(request,Product.class,"yyyy-MM-dd HH:mm");
        //调用业务层接口save
        productService.update(product);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/store/product?operation=list");
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //将数据获取到，封装成一个对象
        Product product = BeanUtil.fillBean(request,Product.class);
        //调用业务层接口save
        productService.delete(product);
        //跳转回到页面list
        response.sendRedirect(request.getContextPath()+"/store/product?operation=list");
    }
    public void login(HttpServletRequest request,HttpServletResponse response){

    }
    public void findAllCity(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //1. 设置响应编码
        response.setContentType("application/json;charset=utf-8");

        //2. 调用service查询
        ProductService productService = new ProductServiceImpl();
        List<City> cities = productService.findAllCity();

        //3. 将集合转换为JSON, 写回到浏览器端
        ObjectMapper objectMapper = new ObjectMapper();
        String json = objectMapper.writeValueAsString(cities);
        response.getWriter().write(json);

        // 将provinces集合转换为JSON字符串, 通过response的输出字符流写回到浏览器端
        //objectMapper.writeValue(response.getWriter(),provinces);

    }
    public void change(HttpServletRequest request,HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String page = request.getParameter("page");
        System.out.println(id+"========================");
        Product product = productService.findById(id);
        Integer productStatus = product.getProductStatus();
        productStatus=productStatus==0?1:0;
//product.setProductStatusStr(productStatus.toString());
        product.setProductStatus(productStatus);
       productService.update(product);
        request.setAttribute("product",product);

        response.sendRedirect(request.getContextPath()+"/store/product?operation=list");

    }
}
